﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SaleLibrary.License;
using System.Net.Mail;
using System.Xml;
using System.Xml.Linq;
namespace SaleLibrary.Tools
{
    public class MailSender
    {
        public static string GetFromMail(string mailName,XElement xml)
        {
            XElement el = xml.Elements("smtpclient").FirstOrDefault(p => p.Attribute("name").Value.Equals(mailName));

            return el.Elements("from").FirstOrDefault<XElement>().Value;
        }

        public static IList<SmtpClient> LoadSmtpClients(string fileName)
        {
            IList<SmtpClient> clients = new List<SmtpClient>();
            XElement root = XElement.Load(fileName);

            IEnumerable<XElement> cfgs = root.Elements("smtpclient");

            foreach (XElement el in cfgs)
            {
                SmtpClient smc = new SmtpClient();
                smc.Port = int.Parse(el.Elements("port").FirstOrDefault<XElement>().Value);
                smc.Host = el.Elements("host").FirstOrDefault<XElement>().Value;

                XElement c = el.Elements("credentials").FirstOrDefault<XElement>();

                string userName = c.Elements("username").FirstOrDefault<XElement>().Value;
                string pass = c.Elements("password").FirstOrDefault<XElement>().Value;

                smc.Credentials = new System.Net.NetworkCredential(userName, pass);
                smc.EnableSsl = false;

                clients.Add(smc);
            }

            return clients;
        }

        public static string GetMailBody(ActiveCode ac)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("您好:");
            sb.Append(Environment.NewLine);
            sb.Append("    这是本产品的注册码:");
            sb.Append(Environment.NewLine);
            sb.Append(ac.Code);
            sb.Append(Environment.NewLine);
            sb.Append("    请在");
            sb.Append(ac.RegPeriod);
            sb.Append("    天内注册,否则实效!");
            sb.Append("    请妥善保管好该激活码，不要外泄!");

            return sb.ToString();
        }


        public static bool SenMail(SmtpClient smtpclient,string address,string subject, string body)
        {

            try
            {
                MailAddress mailAddress = new MailAddress(address);

                MailMessage mail = new MailMessage();


                mail.From = new MailAddress(((System.Net.NetworkCredential)smtpclient.Credentials).UserName);
                mail.To.Add(address);
                mail.Subject = subject;
                mail.Body = body;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                smtpclient.Send(mail);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;
        }


        public SmtpClient SmtpClient { get; set; }

        public string SendAddress { get; set; }
    }
}
